    var associations = [];
    var exportMode = null;
    var data = null;
	
	// LVM
	var onlyIdGlob = null;
	
	function displayActions(value)
	{
		//alert ('coucou324');
		
		// LVM : il ne faut afficher les boutons que pour la ligne pour laquelle on a demandé une construction de fichier.
		//       et si toutes les lignes ne prendre en compte que celles dont les checkboxs sont cochées
		/*
		// Toutes les lignes
		var elements = document.getElementsByClassName("actionButtons");
		
		var element = null;
		var max = elements.length;
		var i = 0;
		
		for (i = 0; i < max; i++)
		{
			element = elements[i];
			element.style.display = (value == true) ? "inline" :"none";
		}
		*/
		//alert ('ici231');
		
		var elt = null, fiche = null, i = 0, checkbox;
		
		//alert ('onlyIdGlob213 = ' + onlyIdGlob);
			
		if (onlyIdGlob === 'All')
		{
			//alert ('tst90');
			
			//alert ('value4 = ' + value);
			
			//alert ('nb ficheall = ' + fiches.length);
			
			// Les boutons 'All' Javascript
			elt = document.getElementById('saveListBtn(All)');
			elt.style.display = (value === true) ? "inline" :"none";
			
			elt = document.getElementById('printListBtn(All)');
			elt.style.display = (value === true) ? "inline" :"none";	
		
			elt = document.getElementById("button_flash(All)");
			
			elt.style.display = (value === true) ? "inline" :"none";
			
			// Toutes les lignes cochées
			i = fiches.length;
				
			while (--i > -1)
			{
				//alert ('jall = ' + i);
			
				fiche = fiches[i];
			
				//alert ('fichejdall = ' + fiche.id);
			
				checkbox = document.getElementById('select_' + fiche.id);
				
				//alert ('checkbox.checked = ' + checkbox.checked);
				
				if (checkbox.checked === true) // Et pas "checked" !!
				{
					//alert ('checkbox checked');
					
					elt = document.getElementById('saveListBtn(' + fiche.id + ')');
					elt.style.display = (value === true) ? "inline" :"none";
					
					elt = document.getElementById('printListBtn(' + fiche.id + ')');
					elt.style.display = (value === true) ? "inline" :"none";
					
					elt = document.getElementById("button_flash(" + fiche.id + ")");
				
					elt.style.display = (value === true) ? "inline" :"none";
				}
			}	
		}
		else if (onlyIdGlob !== null)
		{
			//alert ('ici781');
			//alert ('value = ' + value);
			
			// Une ligne seulement, cochée ou non, à partir du moment où on a cliqué sur le bouton construire sur la ligne
			// Et on force le cochage
			checkbox = document.getElementById('select_' + onlyIdGlob);
			checkbox.checked = "checked";
			
			elt = document.getElementById('saveListBtn(' + onlyIdGlob + ')');
			
			//alert ('nom = ' + 'saveListBtn(' + onlyIdGlob + ')');
			//alert ('elt = ' + elt);
			
			elt.style.display = (value === true) ? "inline" :"none";
			
			elt = document.getElementById('printListBtn(' + onlyIdGlob + ')');
			elt.style.display = (value === true) ? "inline" :"none";
			
			//elts = document.getElementsByName("button_flash(" + onlyIdGlob + ")");
			elt = document.getElementById("button_flash(" + onlyIdGlob + ")");
			
			elt.style.display = (value === true) ? "inline" :"none";
		}
		//elseif (onlyIdGlob == undefined)
		//elseif (onlyIdGlob == 'undefined')
		else if (onlyIdGlob === null)
		{
			//alert ('coucou75');
		
			// Les boutons 'All' Javascript
			elt = document.getElementById('saveListBtn(All)');
			elt.style.display = (value === true) ? "inline" :"none";
			
			elt = document.getElementById('printListBtn(All)');
			elt.style.display = (value === true) ? "inline" :"none";
			
			// Les boutons 'All' pour le flash
			//elt = document.getElementsByName("button_flash(All)");
			elt = document.getElementById("button_flash(All)");
			
			elt.style.display = (value === true) ? "inline" :"none";
			
			// Toutes les lignes
			//alert ('nb fiche5 = ' + fiches.length);
			
			i = fiches.length;
			
			//alert ('value3 = ' + value);
			
			while (--i > -1)
			{
				//alert ('j1 = ' + i);
			
				fiche = fiches[i];
			
				//alert ('fichejd1 = ' + fiche.id);
			
				checkbox = document.getElementById('select_' + fiche.id);
				
				elt = document.getElementById('saveListBtn(' + fiche.id + ')');
				elt.style.display = (value === true) ? "inline" :"none";
				
				elt = document.getElementById('printListBtn(' + fiche.id + ')');
				elt.style.display = (value === true) ? "inline" :"none";
				
				elt = document.getElementById("button_flash(" + fiche.id + ")");
				
				elt.style.display = (value === true) ? "inline" :"none";
			}
			
			// Toutes les checkbox
			i = fiches.length;
			
			while (--i > -1)
			{
				fiche = fiches[i];
	
				checkbox = document.getElementById('select_' + fiche.id);
				
				checkbox.checked = null;
				checkbox.checked = false;
				
				//alert ('i99 = ' + i);
			}
		}
	}
	
	
	function flip(id,element)
	{
		displayActions(false);
		
		if( element.checked )
		{
			document.getElementById("preview_"+id).className = "flip";
		}
		else
		{
			document.getElementById("preview_"+id).className = "";
		}
	}
	
	function getFormatSize(userFormat)
	{
		if ( formatsPixels[userFormat] )
		{
			return formatsPixels[userFormat];
		}
		else
		{
			return formatsPixels.A4;
		}
	}
	
	function getFormatSizeInPt()
	{
		var convert = 2.83, tab = [], prop;
		
		for (prop in allFormats)
		{
			tab[''+prop+''] = [parseInt(allFormats[prop][0] * convert, 10) , parseInt(allFormats[prop][1] * convert, 10)];
			tab[''+prop.toLowerCase()+''] = [parseInt(allFormats[prop][0] * convert, 10) , parseInt(allFormats[prop][1] * convert, 10)];
		}
		
		return tab;
	}
	
	function getFormatSizeInPixels()
	{
		var dpi = 135, convert = dpi / 2.54 / 10, tab = [], prop;
		
		for (prop in allFormats)
		{
			tab[''+prop.toLowerCase()+''] = [parseInt(allFormats[prop][0] * convert, 10) , parseInt(allFormats[prop][1] * convert, 10)];
			tab[''+prop+''] = [parseInt(allFormats[prop][0] * convert, 10) , parseInt(allFormats[prop][1] * convert, 10)];
		}
		
		return tab;
	}
	
	function getFormatLabelBySize(width, height, orientation)
	{
		var distX = 0, distY = 0, prop;
		
		for (prop in formatsPixels)
		{
			if (orientation === 'portrait')
			{
				distX = Math.abs(width - formatsPixels[prop][0]);
				distY = Math.abs(height - formatsPixels[prop][1]);
			}
			else
			{
				distX = Math.abs(width - formatsPixels[prop][1]);
				distY = Math.abs(height - formatsPixels[prop][0]);
			}
			
			if (distX < 10 && distY < 10)
			{
				return prop;
			}
		}
		
		return 'a8';
	};
	
	function getAssocById(id)
	{
		var i = associations.length, current = null;
		
		while (--i > -1)
		{
			current = associations[i];
			
			if (current.id1 === id || current.id2 === id)
			{
				return i;
			}
		}
		
		return -1;
	}
	
	function cleanAssocById(id)
	{
		// srcFormat = null, targetFormat = null, srcPos = null, targetPos = null
		var srcId = -1, targetId = -1, srcSelect = null, targetSelect = null, srcCheckbox = null, targetCheckbox = null, index = getAssocById(id), current  = null;
		
		while (index !== -1)
		{
			current = associations[index];
			srcId = current.id1;
			targetId = current.id2;
				
			srcSelect = document.getElementById('duo_'+srcId);
			targetSelect = document.getElementById('duo_'+targetId);
			srcCheckbox = document.getElementById('select_'+srcId);
			targetCheckbox = document.getElementById('select_'+targetId);
			
			if (srcSelect)
			{
				srcSelect.value = -1;
			}
			
			if (targetSelect)
			{
				targetSelect.value = -1;
			}
			
			if (targetCheckbox)
			{
				targetCheckbox.checked = "";
			}
				
			if (srcCheckbox)
			{
				srcCheckbox.checked = "";
			}
				
			associations.splice (index, 1);
			
			index = getAssocById(id);
		}
	}

	function makeAssoc(srcId, targetId)
	{
		cleanAssocById(srcId);
		cleanAssocById(targetId);
		
		associations.push ({id1: srcId, id2: targetId});
		
		var srcSelect = document.getElementById('duo_'+srcId),
		    targetSelect = document.getElementById('duo_'+targetId),
		
		    srcCheckbox = document.getElementById('select_'+srcId),
		    targetCheckbox = document.getElementById('select_'+targetId),
		
		    srcFormat = document.getElementById('frmt_'+srcId),
		    targetFormat = document.getElementById('frmt_'+targetId),
		
		    srcPos = document.getElementById('pos_'+srcId),
		    targetPos = document.getElementById('pos_'+targetId);
		
		if (targetPos && srcPos)
		{
			targetPos.value = -parseInt(srcPos.value, 10);
		}
		
		if (targetFormat && srcFormat)
		{
			targetFormat.value = srcFormat.value;
		}
		
		if (srcSelect)
		{
			srcSelect.value = targetId;
		}
		
		if (targetSelect)
		{
			targetSelect.value = srcId;
		}
		
		if (targetCheckbox)
		{
			targetCheckbox.checked = "checked";
		}
			
		if (srcCheckbox)
		{
			srcCheckbox.checked = "checked";
		}
	}
	
	function assoc(id)
	{
		displayActions(false);
		
		var element = document.getElementById("duo_"+id);
		
		makeAssoc(parseInt(id, 10), parseInt(element.value, 10));
	}
	
	function eraseCookie(name) 
	{
		createCookie(name, "", -1);
	}
	
	function createCookie(name, value, days) 
	{
		var date, expires;
		
		if (days) 
		{
			date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		}
		else 
		{
			expires = "";
		}
			
		document.cookie = name+"="+value+expires+"; path=/";
	}

	function readCookie(name) 
	{
		var nameEQ = name + "=", ca = document.cookie.split(';'), i, c;
		
		for (i=0;i < ca.length;i++) 
		{
			c = ca[i];
			
			while (c.charAt(0) === ' ')
			{ 
				c = c.substring(1,c.length);
			}
			
			if (c.indexOf(nameEQ) === 0) 
			{
				return c.substring(nameEQ.length,c.length);
			}
		}
		
		return null;
	}
	
	function deleteFiche(id)
	{
		var row = document.getElementById('line_'+id),
		    cookie = readCookie('printList'),
		    list = (cookie === null ) ? new Array() : cookie.split(','),
		    i = list.length,
		    index = -2;
		
		removeFicheById(parseInt(id, 10));
		
		if (row)
		{
			row.parentNode.removeChild(row);
		}
			
		while (--i > -1)
		{
			if (list[i] === id)
			{
				index = i;
			}
		}
		
		if (index > -1)
		{
			list.splice(index,2);
		}
		
		createCookie('printList', list.join(','));
	}
	
	function getFicheAfter(id)
	{
		var i = fiches.length;
		
		while (--i > -1)
		{
			if (fiches[i].id === id)
			{
				if (i + 1 < fiches.length)
				{
					return fiches[i+1];
				}
			}
		}
		
		return null;
	}
	
	function getFicheById(id)
	{
		var i = fiches.length;
		
		while (--i > -1)
		{
			if (fiches[i].id === id)
			{
				return fiches[i];
			}
		}
		
		return null;
	}
	
	function removeFicheById(id)
	{
		var results = [], i = fiches.length;
		
		while (--i > -1)
		{
			if (fiches[i].id === id)
			{
				continue;
			}
				
			results.push(fiches[i]);
		}
		
		fiches = results;
	}
	
	
	function run(onlyId)
	{
		//alert ('toto789');
		
		// LVM : transmettre le onlyId dans _completeHandler pour lancer displayActions(true, onlyId)
		//       mais class Printer non trouvée. Donc variable globale...
		// 3 cas : All, onlyId et undefined (au premier chargement du pool par ex)
		
		onlyIdGlob = onlyId;
		
		//alert ('onlyIdGlob2 = ' + onlyIdGlob);
		
		// Not used : isJPG = document.getElementById('formatJPG').checked
		var i = fiches.length, fiche = null, printer = new Printer(), checkbox = null, duo = null, pos = null, frmt = null, reverse = null, 
		        isPDF = document.getElementById('formatPDF').checked, isPNG = document.getElementById('formatPNG').checked,
		        progress = document.getElementById('progress'),
		        mode = (isPNG) ? 'png' : 'jpeg';
		        
		data = null;
		
		// LVM : ne pas tout enlever par défaut
		//displayActions(false);
		
		if (isPDF)
		{
			mode = 'jpeg';
		}
		
		printer.reset();
		
		//alert ('onlyId = ' + onlyId);
		//alert ('nb fiches = ' + fiches.length);
		
		//alert ('i0 = ' + i);
		
		while (--i > -1)
		{
			//alert ('i1 = ' + i);
			
			fiche = fiches[i];
			
			//alert ('ficheid1 = ' + fiche.id);
			
			checkbox = document.getElementById('select_' + fiche.id);
			reverse = document.getElementById('reverse_' + fiche.id);
			duo = document.getElementById('duo_' + fiche.id);
			pos = document.getElementById('pos_' + fiche.id);
			frmt = document.getElementById('frmt_' + fiche.id);
			
			duo = ( duo === null ) ? null : duo.value;
			pos = ( pos === null ) ? null : pos.value;
			
			if (onlyId !== null)
			{
				//if (fiche.id != onlyId)
				if (fiche.id !== onlyId && onlyId !== 'All')
				{
					// LVM : pourquoi décocher les autres lignes ?
					checkbox.checked = null;
					checkbox.checked = false;
					continue;
				}
				else if (fiche.id === onlyId)
				{
					//alert ('idfiche32 = ' + fiche.id);
					//alert ('onlyid33 = ' + onlyId);
					
					// LVM : on force mais vraiment utile ?
					checkbox.checked = "checked";
				}
			}
			
			if (checkbox.checked)
			{
				printer.addPrintJob(fiche, uiPath, reverse.checked, mode, duo, pos, frmt.value);
			}
		}
		
		progress.innerText = "0%";
		printer.addEventListener(tomahawk_ns.Event.PROGRESS, this, _progressHandler);
		
		printer.addEventListener(tomahawk_ns.Event.COMPLETE, this, _completeHandler);
		
		printer.print();
	}
	
	function _progressHandler(event)
	{
		var printer = event.target, progress = document.getElementById('progress');
		
		progress.innerText = parseInt(printer.getProgression()*100, 10) + "%";
	}
	
	function _completeHandler(event)
	{
		var progress = document.getElementById('progress'), printer = event.target;
		
		progress.innerText = "100%";
		data = printer;
		
		// LVM : ne pas tout afficher si on demande la construction d'une seule fiche (sur une seule ligne)
		//       on utilise une variable globale
		displayActions(true);
	}
	
	function saveList(only)
	{
		exportMode = 'save';
		
		if (isPDF() === true)
		{
			exportPDF(data, only);
		}
		else
		{
			exportRAW(data, only);
		}
	}
	
	function printList(only)
	{
		exportMode = 'print';
		
		if (isPDF() === true)
		{
			noPrintPDF();
			//exportPDF(data);
		}
		else
		{
			exportRAW(data, only);
		}
	}
	
	
	function noPrintPDF()
	{
		alert("pdf files cannot be print");
	}
	
	function noPngForPDF()
	{
		alert("The pdf format does'nt support png, please rebuild while selecting pdf or jpeg mode");
	}
	
	function isPDF()
	{
		var isPDF = document.getElementById('formatPDF').checked;
		
		return (isPDF === true);
	}
	
	function getExport()
	{
		//return null;
		var exports = data.getExport(), i = exports.length, currentExport = null, obj = null, tab = [];
		
		while (--i > -1)
		{
			currentExport = exports[i];
			
			//obj = new Object();
			obj = {};
			
			obj.imgData = currentExport.image.src;
			obj.width = currentExport.image.width;
			obj.height = currentExport.image.height;
			obj.name = currentExport.name+'.'+currentExport.mode;
			tab.push(obj);
		}
		
		return tab;
	}
	
	function exportPDF(printer, only)
	{
		// LVM : not used : , link = '', isLandscape = false
		var doc = null, data = null, image = null, width = 0, height = 0, exports = printer.getExport(), i = exports.length,
		    currentExport = null, fiche = null, format = null, currentName = "", orientation, size = null;
		
		while (--i > -1)
		{
			currentExport = exports[i];
			
			if (currentExport.mode === "png")
			{
				noPngForPDF();
				return;
			}
			
			fiche = currentExport.fiche;
			image = currentExport.image;
			currentName = currentExport.name;
			
			orientation = (fiche.isLandscape === true) ? 'landscape' : 'portrait';
			
			if (only != undefined)
			{
				if (fiche.id !== only)
				{
					continue;
				}
			}
			
			if (currentExport.duo != null && currentExport.duo > -1)
			{
				orientation = (orientation === 'portrait') ? 'landscape' : 'portrait'; 
			}
			
			format = getFormatLabelBySize(image.width, image.height, orientation);
			doc = new jsPDF(orientation, "mm", format, false, formatsInPt);
			width = doc.internal.pageSize.width;
			height = doc.internal.pageSize.height
			
			data = image.src.slice('data:image/jpeg;base64,'.length);
			data = atob(data);
			size = getFormatSizeInPt(fiche.format);
		
			doc.addImage(data, 'JPEG', 0, 0, width, height);
			doc.save(currentName+'.pdf');
		}
	}
	
	// LVM
	function makeblob (blob, name, mode) 
	{
		return function ()
		{	
			saveAs (blob, name + "." + mode);
		};
	}
	
	function exportRAW (printer, only)
	{
		// LVM : unused : data = "", specs = null
		var image = null, exports = printer.getExport(), i = exports.length, currentExport = null, canvas = document.createElement("canvas"),
		    context = canvas.getContext("2d"), isSave = exportMode === 'save', fiche = null, w = null;
		
		while (--i > -1)
		{
			currentExport = exports[i];
			image = currentExport.image;
			fiche = currentExport.fiche;
			
			if (only !== undefined)
			{
				if (fiche.id !== only)
				{
					continue;
				}
			}
			
			if (isSave === true)
			{
				canvas.width = image.width;
				canvas.height = image.height;
				context.drawImage(image,0,0);
				
				if (swfContent.swfSaveData)
				{
					swfContent.swfSaveData(image.src);
				}
				else
				{
					// Warning : Don't make functions within a loop.
					// https://jslinterrors.com/dont-make-functions-within-a-loop
					// Mais cela ne fonctionne pas pour une raison inconnue...
					// canvas.toBlob (makeblob(blob, currentExport.name, currentExport.mode), "image/" + currentExport.mode);
					
					canvas.toBlob(function(blob) {
						saveAs(
							  blob
							, currentExport.name+"."+currentExport.mode
						);
					}, "image/"+currentExport.mode);
				}
			}
			else
			{
				w = window.open(image.src,'_blank', 'width='+image.width+',height='+image.height+",left="+(i*20)+'');
			}
		}
	}
	
	function trace()
	{
		console.log(arguments);
	}
	
    window.onload = function()
    {
		//alert ('titi77');
		
        Tomahawk.run();
		init();
    };
	
	
